package com.logistic.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.logistic.common.Excel;
import com.logistic.common.Files;
import com.logistic.common.UUIDGenerator;
import com.logistic.domain.PickupAddressCN;
import com.logistic.domain.Record;
import com.logistic.service.AddressService;
import com.logistic.service.RecordService;
import com.opensymphony.xwork2.ActionSupport;

public class AddressAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AddressService addressService;
	
	private List list;
	private int limit;
	private int start;
	private int total;

	public String list() {

		if (String.valueOf(start) == null)
			start = 0;
		if (String.valueOf(limit) == null)
			limit = 100;

		// 排序字段
		HttpServletRequest request = ServletActionContext.getRequest();
		String sort = (String) request.getAttribute("sort");
		if (sort == null || sort.equals("")) {
			sort = "id";
		}

		list = addressService.findAddressByUid((String)request.getSession().getAttribute("Uid"));
		if (list == null) {
			total = 0;
		} else {
			total = list.size();
		}

		StringBuffer jsonStr = new StringBuffer("[");

		if (start + limit > total)
			limit = total - start;


		for (int i = start; i < start + limit; i++) {

			PickupAddressCN addr = (PickupAddressCN) list.get(i);

			jsonStr.append("{");

			jsonStr.append("PUaddrId:'").append(addr.getPUaddrId()).append("',");


				jsonStr.append("PUname:'").append(addr.getPUname()).append("',");

				jsonStr.append("PUphoneNum:'").append(addr.getPUphoneNum()).append("',");

			if (addr.getPUsparePhoneNum() == null || addr.getPUsparePhoneNum().equals("null")) {
				jsonStr.append("PUsparePhoneNum:'").append("").append("',");
			} else {
				jsonStr.append("PUsparePhoneNum:'").append(addr.getPUsparePhoneNum().trim())
						.append("',");
			}

			if (addr.getPUarea() == null || addr.getPUarea().equals("")) {
				jsonStr.append("PUarea:'").append("").append("',");
			} else {
				jsonStr.append("PUarea:'").append(addr.getPUarea()).append("',");
			}

			if (addr.getPUaddress() == null || addr.getPUaddress().equals("")) {
				jsonStr.append("PUaddress:'").append("").append("',");
			} else {
				jsonStr.append("PUaddress:'").append(addr.getPUaddress()).append("',");
			}
			
			if (addr.getPUzip() == null || addr.getPUzip().equals("")) {
				jsonStr.append("PUzip:'").append("").append("',");
			} else {
				jsonStr.append("PUzip:'").append(addr.getPUzip()).append("',");
			}


			if (addr.getPUnote() == null || addr.getPUnote().equals("")) {
				jsonStr.append("PUnote:'").append("").append("'");
			} else {
				jsonStr.append("PUnote:'").append(addr.getPUnote()).append("'");
			}

			if (i == start + limit - 1) {
				jsonStr.append("}");
			} else {
				jsonStr.append("},");
			}
		}

		jsonStr.append("]");

		String str = "{totalProperty: " + total + ", root:"
				+ jsonStr.toString() + "}";

		request.setAttribute("result", str);

		return "success";
	}

	/**
	 * 保存
	 */
	public String save() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String PUaddrId = (String) request.getAttribute("PUaddrId");
		
		PickupAddressCN addr = new PickupAddressCN();
		//取得唯一标识码
		addr.setUid((String)request.getSession().getAttribute("Uid"));
		addr.setPUname(this.getPUname());	
		addr.setPUphoneNum(this.getPUphoneNum());
		addr.setPUsparePhoneNum(this.getPUsparePhoneNum());
		addr.setPUarea(this.getPUarea());
		addr.setPUaddress(this.getPUaddress());
		addr.setPUzip(this.getPUzip());
		addr.setPUnote(this.getPUnote());
		

		if (PUaddrId.equals("-1")) { // 新增
			addr.setPUaddrId(UUIDGenerator.getUUID());
			addressService.save(addr);
		//	request.setAttribute("result", "{success:true,message:'ok'}");
		} else { // 修改
			
			addr.setPUaddrId(PUaddrId);
			addressService.modify(addr);
		}

		
		request.setAttribute("result", "{success:true,message:'ok'}");

		return "success";
	}

	/**
	 * 删除
	 */
	public String delete() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String PUaddrId = (String) request.getAttribute("PUaddrId");
		addressService.deleteByIdStr(PUaddrId);

		return "success";
	}



	@SuppressWarnings("rawtypes")
	public List getList() {
		return list;
	}

	@SuppressWarnings("rawtypes")
	public void setList(List list) {
		this.list = list;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	
	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	
	private String PUaddrId;
	private String Uid;
	private String PUname;
	private String PUphoneNum;
	private String PUsparePhoneNum;
	private String PUarea;
	private String PUaddress;
	private String PUzip;
	private String PUnote;

	public String getPUaddrId() {
		return PUaddrId;
	}

	public void setPUaddrId(String pUaddrId) {
		PUaddrId = pUaddrId;
	}
	public String getUid() {
		return Uid;
	}

	public void setUid(String uid) {
		Uid = uid;
	}

	public String getPUname() {
		return PUname;
	}

	public void setPUname(String pUname) {
		PUname = pUname;
	}

	public String getPUphoneNum() {
		return PUphoneNum;
	}

	public void setPUphoneNum(String pUphoneNum) {
		PUphoneNum = pUphoneNum;
	}

	public String getPUsparePhoneNum() {
		return PUsparePhoneNum;
	}

	public void setPUsparePhoneNum(String pUsparePhoneNum) {
		PUsparePhoneNum = pUsparePhoneNum;
	}

	public String getPUarea() {
		return PUarea;
	}

	public void setPUarea(String pUarea) {
		PUarea = pUarea;
	}

	public String getPUaddress() {
		return PUaddress;
	}

	public void setPUaddress(String pUaddress) {
		PUaddress = pUaddress;
	}

	public String getPUzip() {
		return PUzip;
	}

	public void setPUzip(String pUzip) {
		PUzip = pUzip;
	}

	public String getPUnote() {
		return PUnote;
	}

	public void setPUnote(String pUnote) {
		PUnote = pUnote;
	}


	
}
